package cate.game.event.master;

import cate.common.table.d.GDChat;
import cate.common.table.d.GDFunc;
import cate.game.chat.msg.ChatPrivateHistoryResp;
import cate.game.chat.msg.ChatPublicHistoryResp;
import cate.game.chat.msg.MyChatDataResp;
import cate.game.common.msg.ServerInfoResp;
import cate.game.common.time.TimeResp;
import cate.game.cross.msg.CrossLocalResp;
import cate.game.event.GameEventListener;
import cate.game.fight.msg.FightPlayResp;
import cate.game.fight.msg.PositionListResp;
import cate.game.pay.msg.RolePayInitResp;
import cate.game.role.Role;
import cate.game.role.bag.equip.msg.EquipInitResp;
import cate.game.role.bag.furniture.msg.FurnitureInitResp;
import cate.game.role.bag.gem.msg.GemInitResp;
import cate.game.role.bag.hero.msg.HeroInitResp;
import cate.game.role.bag.heropart.msg.HeroPartInitResp;
import cate.game.role.bag.inscription.msg.InscriptionInitResp;
import cate.game.role.bag.kitchenware.msg.KitchenwareInitResp;
import cate.game.role.bag.num.msg.NumResInitResp;
import cate.game.role.bag.prop.msg.ContinuePackDataResp;
import cate.game.role.bag.prop.msg.PackUseTimesDataResp;
import cate.game.role.bag.prop.msg.PropInitResp;
import cate.game.role.bag.rune.msg.RuneInitResp;
import cate.game.role.bag.spice.msg.SpiceInitResp;
import cate.game.role.bag.storehouse.msg.StoreHouseInitResp;
import cate.game.role.base.msg.RoleBaseInitResp;
import cate.game.role.farm.dailyfb.msg.DailyFbDataResp;
import cate.game.role.farm.forbidden.msg.ForbiddenAllMonsterResp;
import cate.game.role.farm.forbidden.msg.ForbiddenDataResp;
import cate.game.role.farm.guildBoss.msg.MyGuildBossDataResp;
import cate.game.role.farm.guildFairyland.msg.GuildFairyInfoResp;
import cate.game.role.friend.msg.FriendInitResp;
import cate.game.role.mail.msg.MailMyResp;
import cate.game.role.note.msg.NoteResp;
import cate.game.role.pvp.throne.msg.TBBaseResp;
import cate.game.role.res.guild.msg.GuildDetailResp;
import cate.game.role.res.guild.msg.MyGuildResp;
import cate.game.role.res.hero.introduction.msg.AllStarGetResp;
import cate.game.role.res.hero.introduction.msg.HeroIntroductionDataResp;
import cate.game.role.res.hero.introduction.msg.HeroStarStatusResp;
import cate.game.role.show.msg.RoleShowResp;
import cate.game.role.system.msg.RoleSystemResp;
import cate.game.role.world.invite.msg.InviteConfResp;
import cate.game.train.hero.msg.HeroRebornDataResp;
import cate.game.train.rune.msg.RuneResResp;
import cate.game.world.msg.RoleWorldDataResp;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RoleInitListener implements GameEventListener<RoleInitEvent> {
    @Override
    public void onEvent(RoleInitEvent gameEvent) {
        long start = System.currentTimeMillis();
        Role role = gameEvent.role;
        role.entering = false;
        safe(() -> role.send(new ServerInfoResp(role.getGame())), "ServerInfoResp");
        safe(() -> role.send(new TimeResp(role.getGame())), "TimeResp");
        safe(() -> role.getStatus().noticeUpdate(), "status.noticeUpdate");
        safe(() -> role.send(new RoleBaseInitResp().data(role.getBase())), "RoleBaseInitResp");
        safe(() -> role.send(new RolePayInitResp((role.getPay()))), "RolePayInitResp");
        safe(() -> role.send(new RoleSystemResp(role)),"RoleSystemResp");
        safe(() -> role.send(new MyChatDataResp(role)), "MyChatDataResp");
        safe(() -> role.send(new NoteResp(role)), "NoteResp");
        safe(() -> role.send(new FriendInitResp(role)), "FriendInitResp");
        safe(() -> role.send(new NumResInitResp(role)), "NumResInitResp");
        safe(() -> role.send(new PropInitResp(role)), "PropInitResp");
        safe(() -> role.send(new PackUseTimesDataResp(role)), "PackUseTimesDataResp");
        safe(() -> role.send(new HeroInitResp(role)), "HeroInitResp");
        safe(() -> role.send(new HeroPartInitResp(role)), "HeroPartInitResp");
        safe(() -> role.send(new EquipInitResp(role)), "EquipInitResp");
        safe(() -> role.send(new RuneInitResp(role)), "RuneInitResp");
        safe(() -> role.send(new FurnitureInitResp(role)), "FurnitureInitResp");
        safe(() -> role.send(new GemInitResp(role)), "GemInitResp");
        safe(() -> role.send(new StoreHouseInitResp(role)), "StoreHouseInitResp");
        safe(() -> role.send(new KitchenwareInitResp(role)), "KitchenwareInitResp");
        safe(() -> role.sendNow(new ContinuePackDataResp(role)), "ContinuePackDataResp");
        safe(() -> role.send(new MailMyResp(role)),"MailMyResp");
        safe(() -> role.send(new HeroRebornDataResp(role.getRes().reborn)),"HeroRebornDataResp");
        safe(() -> role.send(new RuneResResp(role.getRes().rune)), "RuneResResp");
        safe(() -> role.send(new HeroIntroductionDataResp(role)), "HeroIntroductionDataResp");
        safe(() -> role.send(new AllStarGetResp(role.getRes().heroIntroduction.allStarGots)), "AllStarGetResp");
        safe(() -> role.send(new SpiceInitResp(role)), "SpiceInitResp");
        safe(() -> role.send(new PositionListResp(role)),"PositionListResp");
        safe(() -> role.send(new InscriptionInitResp(role)), "InscriptionInitResp");
        safe(() -> role.send(new MyGuildResp(role.getRes().guild)), "MyGuildResp");
        safe(() -> role.send(new GuildDetailResp(role.getRes().guild.getGuild())), "GuildDetailResp");
        safe(() -> {
            if (role.getRes().guild.checkGuild().ok()) {
                role.sendNow(new MyGuildBossDataResp(role));
            }
        }, "guild.noticeUpdate");
        safe(() -> {
            if (role.getRes().inscription.isOpen()) {
                role.getRes().inscription.noticeUpdate();
            }
        }, "inscription.noticeUpdate");
        safe(() -> role.send(new DailyFbDataResp(role)), "DailyFbDataResp");
        safe(() -> role.send(new RoleShowResp(role)),"RoleShowResp");
        safe(() -> role.send(new ChatPublicHistoryResp(GDChat.Channel.GROUP, role)),"ChatPublicHistoryResp");
        safe(() -> role.send(new ChatPrivateHistoryResp(role)),"ChatPrivateHistoryResp");
        safe(() -> role.send(new CrossLocalResp(role.getGame().config.isCrossEnabled(), role.getGame().cross.getWorldLevel())), "CrossLocalResp");
        safe(() -> {
            if (role.getFarm().forbidden.isOpen()) {
                role.sendNow(new ForbiddenAllMonsterResp(role));
                role.sendNow(new ForbiddenDataResp(role));
            }
        }, "forbidden.noticeUpdate");
        safe(() -> role.getRes().composePO.itemCompose.noticeUpdate(), "itemCompose.noticeUpdate");
        safe(() -> role.send(new GuildFairyInfoResp(role)), "GuildFairyInfoResp");
        safe(() -> sendActivity(role), "sendActivity(role)");

        //任务
        safe(() -> role.getTask().initSend(), "getTask().initSend()");
        safe(() -> role.getPay().gift.noticeUpdate(), "getPay().gift.noticeUpdate()");

        safe(() -> role.getHistory().action.login(), "getHistory().action.login()");

        safe(() -> role.getFarm().adventure.noticeUpdate(), "getFarm().adventure.noticeUpdate()");
        safe(() -> role.getFarm().tower.noticeUpdate(), "getFarm().towers.noticeUpdate()");
        safe(() -> role.getFarm().spiceFb.noticeUpdate(), "getFarm().spiceFb.noticeUpdate()");
        safe(() -> role.getFarm().island.noticeUpdate(), "getFarm().island.noticeUpdate()");
        safe(() -> role.getFarm().rank.noticeUpdate(), "getFarm().rank.noticeUpdate()");
        safe(() -> role.getFarm().pk.noticeUpdate(), "getFarm().pk.noticeUpdate()");
        safe(() -> role.getFarm().endless.noticeUpdate(), "endless");
        safe(() -> role.getFarm().udgPalace.noticeUpdate(), "udgpalace noticeupate");

        safe(() -> role.getRes().leader.hallows.noticeUpdate(), "getRes().leader.hallows.noticeUpdate()");
        safe(() -> role.getRes().raceBadge.noticeUpdate(), "getRes().raceBadge.noticeUpdate()");
        safe(() -> role.getRes().furnitureCall.noticeUpdate(), "getRes().furnitureHome.noticeUpdate()");
        safe(() -> role.getRes().callPO.refresh(), "getRes().call.refresh()");
        safe(() -> role.getRes().callPO.noticeCallDataUpdate(), "getRes().call.noticeCallDataUpdate()");
        safe(() -> role.getRes().heroIntroduction.noticeUpdate(), "getRes().heroIntroduction.noticeUpdate");
        safe(() -> role.getRes().worldTree.heroShare.noticeUpdate(), "getRes().worldTree.heroShare.noticeUpdate()");
        safe(() -> role.getRes().treasureTurntables.noticeUpdate(), "treasureTurntable");
        safe(() -> role.getRes().spiceCall.noticeUpdate(), "getRes().spiceCall.noticeUpdate()");
        safe(() -> role.getRes().expireNotice.noticeUpdate(), "getRes().expireNotice.noticeUpdate()");
        safe(() -> role.getRes().kingSacrifice.noticeUpdate(), "getRes().kingSacrifice.noticeUpdate()");
        safe(() -> role.getRes().goldMall.noticeUpdate(), "goldmall notice");
        safe(() -> role.getRes().formations.noticeUpdate(), "formations.notice");
        safe(() -> role.getRes().magicContract.noticeUpdate(), "magicContract.notice");
        safe(() -> role.getRes().forge.noticeUpdate(), "forge.notice");
        safe(() -> role.getRes().watchAdvertisement.noticeUpdate(), "watchAdvertisement.notice");
        safe(() -> role.getRes().farm.noticeUpdate(), "farm.notice");
        safe(() -> role.getRes().achievement.noticeUpdate(), "achievement.notice");
        safe(() -> role.getPvp().videoHall.noticeUpdate(), "getPvp().videoHall.noticeUpdate()");
        safe(() -> role.getPvp().pointRace.liteNotice(), "getPvp().pointRace.liteNotice()");
        safe(() -> role.getPvp().eliteRace.liteNotice(), "getPvp().eliteRace.liteNotice()");

        safe(() -> role.getRes().channelGift.noticeUpdate(), "getRes().channelGift.noticeUpdate()");
        safe(() -> role.getRes().worldTree.addition.noticeUpdate(), "getRes().worldtree.addition.notice");
        safe(() -> role.getRes().findBack.noticeUpdate(), "getRes().findBack.noticeUpdate()");
        safe(() -> role.getRes().crystallDraw.noticeUpdate(), "crystalldraw noticeupdate");
        safe(() -> role.getRes().lubiItem.noticeUpdate(),"lubi noticeupdate");
        safe(() -> role.getRes().revelryDraw.noticeUpdate(),"revelryDraw noticeUpdate");
        safe(() -> role.getActivity().superFirstPay.noticeUpdate(), "getActivity().superFirstPay.noticeUpdate()");
        safe(() -> role.getPvp().ladder.noticeUpdate(false), "getPvp().ladder.noticeUpdate()");
        safe(() -> role.getPvp().minecraft.challengePO.noticeUpdate(), "getPvp().minecraft.challengePO.noticeUpdate()");
        safe(() -> role.getPvp().minecraft.gatherPO.onRoleInit(), "getPvp().minecraft.gatherPO.noticeUpdate()");
        safe(() -> role.getRes().guild.treasureHouse.initSend(), "getRes().guild.treasureHouse.initSend()");
        safe(() -> role.getPvp().guildWar.noticeUpdate(), "getPvp().guildWar.noticeUpdate()");
        safe(() -> role.getPvp().championRace.liteNotice(), "champion.listNotice");

        safe(() -> role.getPay().tradeMall.sendInitInfos(), "getPay().tradeMall.sendInitInfos()");
        safe(() -> role.getFarm().crossBoss.initSend(), "getFarm().crossBoss.initSend()");
        safe(() -> role.getPvp().jungle.initSend(), "getPvp().jungle.initSend()");
        safe(() -> role.getPvp().arena.noticeUpdate(), "getPvp().arena.noticeUpdate()");
        safe(() -> role.send(new TBBaseResp(role.getGame())), "TBBaseResp");
        safe(() -> role.getPvp().throne.noticeUpdate(), "getPvp().throne.noticeUpdate()");
        safe(() -> role.getPvp().gradePvp.noticeUpdate(), "gradepvp.noticeUpdate");
        safe(() -> role.getRes().holyPet.initSend(), "getRes().holyPet.initSend()");
        safe(() -> role.getRes().hoard.noticeUpdate(),"hoard noticeUpdate");
        safe(() -> RoleWorldDataResp.doSend(role), "RoleWorldDataResp");
        sendFightPlayResp(role);
        role.send(new HeroStarStatusResp(role.getRes().heroIntroduction.heroStarGots));
        role.send(new InviteConfResp());
        log.info("{}初始化game完成, cost={} ms", role.getBase().name, System.currentTimeMillis() - start);
    }

    // 自己报自己的错，放过别人的代码吧
    private static void safe(Runnable runnable, String description) {
        try {
            runnable.run();
        } catch (Exception e) {
            log.error("{} 执行发生了错误", description);
            log.error("", e);
        }
    }

    private void sendActivity(Role role) {
        role.getActivity().initSend();
    }

    private void sendFightPlayResp(Role role) {
        FightPlayResp.sendWhenFighting(role, GDFunc.ADVENTURE);
        FightPlayResp.sendWhenFighting(role, GDFunc.ISLAND);
        FightPlayResp.sendWhenFighting(role, GDFunc.SPICE_FB);
        FightPlayResp.sendWhenFighting(role, GDFunc.TOWER);
        FightPlayResp.sendWhenFighting(role, GDFunc.POINT_RACE);
        FightPlayResp.sendWhenFighting(role, GDFunc.WORLD_BOSS);
        FightPlayResp.sendWhenFighting(role, GDFunc.FORBIDDEN_C1);
        FightPlayResp.sendWhenFighting(role, GDFunc.FORBIDDEN_C2);
        FightPlayResp.sendWhenFighting(role, GDFunc.FORBIDDEN_C3);
        FightPlayResp.sendWhenFighting(role, GDFunc.FORBIDDEN_C4);
        FightPlayResp.sendWhenFighting(role, GDFunc.FORBIDDEN_C5);
        FightPlayResp.sendWhenFighting(role, GDFunc.FORBIDDEN_C6);
        FightPlayResp.sendWhenFighting(role, GDFunc.ENDLESS);
        FightPlayResp.sendWhenFighting(role, GDFunc.UDG_PALACE);
        FightPlayResp.sendWhenFighting(role, GDFunc.PK);
        FightPlayResp.sendWhenFighting(role, GDFunc.BATTLE_ROYALE);
        FightPlayResp.sendWhenFighting(role, GDFunc.BATTLE_ROYALE_HIDE);
        FightPlayResp.sendWhenFighting(role, GDFunc.ADVENTURE_BOSS);
        FightPlayResp.sendWhenFighting(role, GDFunc.GOD_TREE);
        FightPlayResp.sendWhenFighting(role, GDFunc.GuildFunc.WAR);
        FightPlayResp.sendWhenFighting(role, GDFunc.DESTINY_GATE);
        FightPlayResp.sendWhenFighting(role, GDFunc.GRADE_PVP);
        FightPlayResp.sendWhenFighting(role, GDFunc.DAILY_FB);
        FightPlayResp.sendWhenFighting(role, GDFunc.LADDER_RACE);
        FightPlayResp.sendWhenFighting(role, GDFunc.LOST_DREAMLAND);
        FightPlayResp.sendWhenFighting(role, GDFunc.THRONE_TRIAL);
        FightPlayResp.sendWhenFighting(role, GDFunc.THRONE_CHAMPION_BOSS);
        FightPlayResp.sendWhenFighting(role, GDFunc.THRONE_BOSS);
        FightPlayResp.sendWhenFighting(role, GDFunc.MOONCAKE_C1);
        FightPlayResp.sendWhenFighting(role, GDFunc.MOONCAKE_C2);
        FightPlayResp.sendWhenFighting(role, GDFunc.MOONCAKE_C3);
        FightPlayResp.sendWhenFighting(role, GDFunc.MOONCAKE_C4);
        FightPlayResp.sendWhenFighting(role, GDFunc.MOONCAKE_C5);
        FightPlayResp.sendWhenFighting(role, GDFunc.MOONCAKE_C6);
        FightPlayResp.sendWhenFighting(role, GDFunc.NEW_HERO_TRIAL);
        FightPlayResp.sendWhenFighting(role, GDFunc.BOSS_TRAIN);
        FightPlayResp.sendWhenFighting(role, GDFunc.M_TOWER);
    }
}
